/**
 * 首页管理
 * @Author: Damen
 * @Data: Created in 2018/3/18
 */
var urlParams   = {loginAction: webRoot + "/login/",
            loginoutAction: webRoot + "/login/loginout"};

$(function () {
    $('#f-nav').tooltip({
        type: 'slideMove'
    });
    initNav();
    if(session.userInfo&&session.userInfo.id){
        $("#navTab2").show()
        $("#navTab3").show()
        $("#navTab1").show()
    }else{
        $("#navTab2").hide()
        $("#navTab3").hide()
        $("#navTab1").hide()
    }
    initEvent();
});

function initNav(){
    var userInfo = session.userInfo;
    if(userInfo){
        $(".user-action").hide();
        $(".user-info").show();
        if(userInfo.userName){
            $(".user-name").text(userInfo.userName);
        }else{
            $(".user-name").text(userInfo.phoneNumber);
        }

    }
}

/**
 * 添加监听
 */
function initEvent(){
    $(".f-nav li").on("click",function(li){
        $("#iframe-page-content").attr('src',webRoot+$(this).attr("action"));
    });

    $(".user-login").on("click",function(){
        window.location.href = webRoot+"login/user";
    });
    $(".user-register").on("click",function(){
        window.location.href = webRoot+"login/register";
    });
    $(".editpass").on("click",function(){
        window.location.href = webRoot+"login/editpass";
    });
    $(".editUserInfo").on("click",function(){
        window.location.href = webRoot+"user/userInfo";
    });
    $(".loginout").on("click",function(){
        $.ajax({
            type: 'post',
            url: urlParams.loginoutAction,
            cache: false,
            data: null,
            // dataType: 'json',
            success: function (data) {
                if(data == "success"){
                    Message.show("退出成功");
                    setTimeout(function(){
                        window.location.reload()
                    },1000);
                }
            },
            error:function(){
            }
        });
    });

    $("#iframe-page-content").on("load",function(){
        $("#iframe-page-content").css("height",600);
        interval = window.setInterval("resetIframe()", 200);
    });
}

var isChange = 0;
var interval = 0;
function resetIframe(){
    if(isChange==0){
        var iframeHeight = $("#iframe-page-content").get(0).contentWindow.document.body.scrollHeight;
        if(iframeHeight!=600){
            isChange = 1;
            iframeHeight = iframeHeight<600?600:iframeHeight;
            $("#iframe-page-content").css("height",iframeHeight);
            window.clearInterval(interval);
        }
    }
}

function navClickAction(tabNum,param){
    var action = $("#navTab"+tabNum).attr("action");
    if(param){
        $("#navTab"+tabNum).attr("action",action+param);
    }
    $("#navTab"+tabNum).click();
    if(param){
        $("#navTab"+tabNum).attr("action",action);
    }
}